package com.gxa.pingan.controller;

import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.pojo.Coupon;
import com.gxa.pingan.service.CouponService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/coupon")
@Api(value = "优惠券操作的控制器", tags = "优惠券操作接口的api")
public class CouponController {

    @Autowired
    private CouponService couponService = null;

    /**
     * 分页获取优惠券列表查
     *
     * @Author lirucong
     * @Date 18:31 2021/10/25
     */
    @GetMapping("/getCpnList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "Integer"),
            @ApiImplicitParam(name = "size", value = "记录条数条数", paramType = "Integer")
    })
    @Transactional
    @ApiOperation(value = "查询全部系统的优惠券接口", notes = "获取所有的优惠券", httpMethod = "get")
    public Map getList(Integer page, Integer size) {
        List<Coupon> data = couponService.getList(page, size);
        Integer count = couponService.getListCount();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("0");
        jsonResult.setData(data);
        jsonResult.put("count",count);
//        jsonResult.
        return jsonResult.getDataMap();
    }

    /**
     * 添加优惠券
     *
     * @Author lirucong
     * @Date 18:16 2021/10/25
     */
    @ApiOperation(value = "添加优惠券接口", notes = "添加优惠券接口", httpMethod = "post")
    @PostMapping("/add")
    public Map addCoupon(@RequestBody Coupon coupon) throws Exception {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("添加成功");
        couponService.add(coupon);
        return jsonResult.getDataMap();
    }

    /**
     * 修改优惠券
     *
     * @Author lirucong
     * @Date 18:15 2021/10/25
     */
    @ApiOperation(value = "修改优惠券接口", notes = "修改优惠券接口", httpMethod = "post")
    @PostMapping("/updateCpn")
    public Map updateCpn(@RequestBody Coupon coupon) throws BusinessException {
        couponService.update(coupon);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    /**
     * 修改优惠券状态
     *
     * @Author lirucong
     * @Date 18:15 2021/10/25
     */
    @ApiOperation(value = "修改优惠券状态接口", notes = "修改优惠券状态接口", httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "status", paramType = "Boolean")
    })
    @PostMapping("/setStatus")
    public Map setStatus(Long id,Boolean status) throws BusinessException {
        couponService.updateStatus(id, status);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("设置成功");
        jsonResult.setCode("0");
        return jsonResult.getDataMap();
    }

    /**
     * 得到发送方式和获取方式，此处是关联的
     * @Author  lirucong
     * @Date 9:23 2021/10/26
     */
    @ApiOperation(value = "得到发送方式和获取方式，此处是关联的", notes = "得到发送方式和获取方式，此处是关联的", httpMethod = "get")
    @GetMapping("/getMethod")
    public Map getMethod() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        List<Map<String, Object>> method = couponService.getMethod();
        jsonResult.setMsg("成功");
        jsonResult.setData(method);

        return jsonResult.getDataMap();
    }
}
